#ifndef __BSP_UART_H
#define __BSP_UART_H

#include "gd32f4xx_gpio.h"
#include "gd32f4xx_usart.h"
#include <stdint.h>   
#include "ringbuffer.h"

#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"

#define USART0_TX_SIZE 0xff
#define RX_SIZE 0xff

typedef struct _bsp_uart_driver bsp_uart_driver_t;

enum uart_cmd 
{
    IOCTRL_CMD = 0,
    IOCTRL_GET_RX_BUFF_SIZE,
    IOCTRL_GET_RX_BUFF_BYTE,
    IOCTRL_SET_TX_BUFF,
};

struct uart_ops 
{
    uint32_t (*ioctrl)(bsp_uart_driver_t* drv, uint8_t cmd, uint32_t* val);
    int16_t (*read)(bsp_uart_driver_t* drv, uint8_t* buff, uint16_t len);
    int16_t (*write)(bsp_uart_driver_t* drv, uint8_t* buff, uint16_t len);
    int16_t (*irq_cb)(bsp_uart_driver_t* drv, uint8_t* buff, uint16_t len);
};

#pragma pack(1)
struct _protocol
{
    uint8_t buff[64U];
    uint8_t size;
};
#pragma pack()

struct uart_port 
{
    //ring_buffer r_buff;  // DMA接收缓存
    //ring_buffer t_buff;  // DMA发送缓存

    //uint8_t r_sem;  // 表示接收或者发送完成信号
    //uint8_t t_sem;  // 表示接收或者发送完成信号

    QueueHandle_t tx_queue; // 接收队列

    QueueHandle_t rx_queue; // 接收队列
    uint8_t rx_buff[RX_SIZE]; // dma接收缓存
};

struct _bsp_uart_driver
{
    uint32_t uart_id;
    struct uart_port port;
    struct uart_ops ops;
};

extern bsp_uart_driver_t *lora_drv(void);
extern bsp_uart_driver_t *scan_drv(void);

int bsp_uart_register_driver(bsp_uart_driver_t* drv);
void bsp_uart_unregister_driver(bsp_uart_driver_t* drv);


#endif
